<template>
  <div class="home">
    <!-- topbar -->
    <div class="header">
      <div class="header_box_title">
        <h3 class="header_title">叩丁狼电商运营平台</h3>
        <el-icon size="20" v-model="iscollapse" @click="iscollapse = !iscollapse">
          <Menu />
        </el-icon>
      </div>
      <div class="header_right">
        <el-dropdown @command="handlerCommand">
          <span class="header_right_uname">
            {{ username }}
            <el-icon>
              <arrow-down />
            </el-icon>
          </span>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item command="/home">首页</el-dropdown-item>
              <el-dropdown-item command="/logout">退出</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </div>
    <div class="section_contain">
      <!-- 侧边菜单栏 -->
      <div class="aside_menus">
        <el-menu class="el-menu-vertical-list" :default-active="defaultActive" active-text-color="#409EFF"
          :unique-opened="true" background-color="#304156" text-color="#fff" :collapse="iscollapse" :router="true">
          <el-menu-item index="/home">
            <template #title>
              <el-icon>
                <House />
              </el-icon>
              <span>首页</span>
            </template>
          </el-menu-item>
          <el-sub-menu v-for="item in filterMenus" :key="item.id" :index="'/' + item.name">
            <template #title>
              <el-icon>
                <Menu />
              </el-icon>
              <span>{{ item.title }}</span>
            </template>
            <el-menu-item v-show="el.hidden === 0" v-for="el in item.children" :key="el.id"
              :index="'/' + item.name + '/' + el.name">
              <el-icon>
                <Menu />
              </el-icon>
              <span>{{ el.title }}</span>
            </el-menu-item>
          </el-sub-menu>
        </el-menu>
      </div>
      <div class="aside_menus_content">
        <!-- 面包屑 -->
        <div class="toast_box">
          <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <template v-if="secondRouteName !== '首页'">
              <el-breadcrumb-item :to="{ path: primaryRoutePath }">{{
                primaryRouteName
              }}</el-breadcrumb-item>
              <el-breadcrumb-item>{{ secondRouteName }}</el-breadcrumb-item>
            </template>
          </el-breadcrumb>
        </div>
        <div class="router_box">
          <!-- 右侧二级路由出口 -->
          <RouterView v-if="isRouterAlive" />
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
// 引入图标
import { Menu, ArrowDown, House } from "@element-plus/icons-vue";
import { ref, computed, watch, nextTick, provide } from "vue";

// 引入store
import useUserInfoStore from "@/stores/useInfo";
// 引入路由出口
import { RouterView, useRoute } from "vue-router";
import router from "@/router";
import Cookies from "js-cookie";
// 菜单栏高亮
let defaultActive = ref("/home");
// 一级菜单路径等路由元信息
let primaryRoutePath = ref("");
let primaryRouteName = ref("");
let secondRouteName = ref("");
watch(
  useRoute(),
  (newVal) => {
    const { meta, fullPath } = newVal;
    defaultActive.value = fullPath;
    primaryRoutePath.value = meta.primaryRoutePath as string;
    primaryRouteName.value = meta.primaryRouteName as string;
    secondRouteName.value = meta.secondRouteName as string;
  },
  { immediate: true }
);
const userInfoStore = useUserInfoStore();
const username = userInfoStore.username;
let filterMenus = computed(() => {
  return userInfoStore.filterMenus;
});
/* const arr = userInfoStore.menus;
// 筛选出一级菜单
let newArr = arr.filter((item) => {
  item.children = [];
  return item.parentId === 0;
});
// console.log(newArr);
// 添加二级菜单
let MenuList = newArr.map((item) => {
  arr.forEach((item2) => {
    if (item2.parentId === item.id) {
      item.children?.push(item2);
      // console.log(item.children);
    }
  });
  return item;
});
console.log(MenuList); */
//页面刷新
const isRouterAlive = ref(true);
const reload = () => {
  isRouterAlive.value = false;
  nextTick(() => {
    isRouterAlive.value = true
  })
}
provide("reload", reload);
// 折叠开关
const iscollapse = ref(false);
const handlerCommand = (command: string | number | object) => {
  if (command === "/home") {
    router.push("/home");
  } else if (command === "/logout") {
    Cookies.remove("token");
    userInfoStore.updateUserInfo([], "");
    router.push("/login");
  }
};
</script>

<style lang="less" scoped>
.home {
  .header {
    height: 70px;
    background-color: #408eff;
    color: #fff;
    display: flex;
    justify-content: space-between;
    align-items: center;

    .header_box_title {
      display: flex;
      align-items: center;

      .header_title {
        font-size: 24px;
        padding: 0 20px;
      }
    }

    .header_right {
      padding-right: 20px;

      .header_right_uname {
        display: flex;
        align-items: center;
        color: #fff;
      }
    }

    :deep(.el-tooltip__trigger) {
      outline: none;
    }
  }

  .section_contain {
    display: flex;

    .aside_menus {
      height: calc(100vh - 70px);
      background-color: #304156;

      .el-menu {
        overflow-y: auto;
        height: 100%;

        &::-webkit-scrollbar {
          width: 0;
        }
      }

      .el-menu-vertical-list:not(.el-menu--collapse) {
        width: 250px;
      }
    }

    .aside_menus_content {
      flex: 1;

      .toast_box {
        height: 45px;
        padding: 15px 20px;
        border-bottom: 1px solid #eee;
      }

      .router_box {
        width: calc(100vw - 250px);
        height: calc(100vh - 115px);
        overflow-y: scroll;
      }
    }
  }
}

::-webkit-scrollbar {
  width: 0;
}
</style>
